package com.banciyuan.bcywebview.biz.goods.store;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleSmoothActivity;
import com.banciyuan.bcywebview.biz.detail.goods.GoodsDetailActivity;
import com.banciyuan.bcywebview.biz.topic.TopicDetailActivity;
import com.banciyuan.bcywebview.biz.write.photoselecotor.util.CommonUtils;
import com.banciyuan.bcywebview.utils.common.UIUtils;
import com.banciyuan.bcywebview.utils.currency.CurrencyUtil;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.DetailGoods;
import de.greenrobot.daoexample.model.GoodsChoice;
import de.greenrobot.daoexample.model.GoodsChoiceItem;

/**
 * Created by squall on 2016/11/3.
 */

public class GoodsStoreAdapter extends BaseAdapter {

    private List<GoodsChoice> dlist = new ArrayList<>();
    private Context mContext;
    private LayoutInflater inflater;
    private ImageLoader mImageLoader;
    private int GOODS_TYPE = 0;
    private int SPECIAL_TYPE = 1;
    private int WORK_TYPE = 2;

    public GoodsStoreAdapter(List<GoodsChoice> dlist, Context mContext) {
        this.dlist = dlist;
        this.mContext = mContext;
        this.inflater = LayoutInflater.from(mContext);
        mImageLoader = ImageLoader.getInstance();
    }

    @Override
    public int getCount() {
        return dlist.size();
    }

    @Override
    public Object getItem(int position) {
        return dlist.get(position);
    }


    @Override
    public int getItemViewType(int position) {
        String type = dlist.get(position).getType();
        if ("special".equals(type))
            return SPECIAL_TYPE;
        if ("work".equals(type))
            return WORK_TYPE;
        return GOODS_TYPE;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int type = getItemViewType(position);
        GoodsChoice goodsChoice = dlist.get(position);
        if (type == GOODS_TYPE) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.goods_store_item, parent, false);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            setConvertViewPadding(position, convertView);
            dealGoodsData(goodsChoice.getData(), viewHolder, 0);
            dealGoodsData(goodsChoice.getNextGoodsData(), viewHolder, 1);
        }
        if (type == SPECIAL_TYPE) {
            SpecialViewHolder specialViewHolder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.goods_store_special_item, parent, false);
                specialViewHolder = new SpecialViewHolder(convertView);
                convertView.setTag(specialViewHolder);
            } else {
                specialViewHolder = (SpecialViewHolder) convertView.getTag();
            }
            setConvertViewPadding(position, convertView);
            dealSpecialData(goodsChoice, specialViewHolder);
        }
        if (type == WORK_TYPE) {
            WorkViewHolder workViewHolder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.goods_store_work_item, parent, false);
                workViewHolder = new WorkViewHolder(convertView);
                convertView.setTag(workViewHolder);
            } else {
                workViewHolder = (WorkViewHolder) convertView.getTag();
            }
            setConvertViewPadding(position, convertView);
            dealWorkData(goodsChoice, workViewHolder);
        }
        return convertView;
    }

    private void setConvertViewPadding(int position, View convertView) {
        if (position == 0)
            convertView.setPadding(convertView.getPaddingLeft(), UIUtils.dip2px(12, mContext), convertView.getPaddingRight(), convertView.getPaddingBottom());
        else
            convertView.setPadding(convertView.getPaddingLeft(), UIUtils.dip2px(24, mContext), convertView.getPaddingRight(), convertView.getPaddingBottom());
    }

    private void dealSpecialData(GoodsChoice goodsChoice, SpecialViewHolder specialViewHolder) {
        final GoodsChoiceItem goodsChoiceData = goodsChoice.getData();
        specialViewHolder.tvSpecialName.setText(Html.fromHtml(goodsChoiceData.getTitle()));
        mImageLoader.displayImage(goodsChoiceData.getCover(), specialViewHolder.ivSpecialCover, ImageOptions.options, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                float scale = (float) loadedImage.getHeight() / (float) loadedImage.getWidth();
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
                params.width = CommonUtils.getWidthPixels((Activity) mContext);
                params.height = (int) (params.width * scale);
                view.setLayoutParams(params);
                ((ImageView) view).setImageBitmap(loadedImage);
            }
        });
        ((View) specialViewHolder.ivSpecialCover.getParent()).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoUtil.gotoAct(mContext, TopicDetailActivity.class, Integer.valueOf(goodsChoiceData.getSpecial_id()));
            }
        });
    }

    private void dealWorkData(GoodsChoice goodsChoice, WorkViewHolder workViewHolder) {
        final GoodsChoiceItem goodsChoiceData = goodsChoice.getData();
        mImageLoader.displayImage(goodsChoiceData.getCover(), workViewHolder.ivWorkCover, ImageOptions.options);
        workViewHolder.tvGoodsCount.setText(String.format(mContext.getString(R.string.goods_category_format), goodsChoiceData.getGoods_count()));
        workViewHolder.tvWorkName.setText(Html.fromHtml(goodsChoiceData.getReal_name()));
        List<DetailGoods> goods_data = goodsChoiceData.getGoods_data();
        int size = goods_data.size() > 3 ? 3 : goods_data.size();
        for (int i = 0; i < size; i++) {
            final DetailGoods detailGoods = goods_data.get(i);
            workViewHolder.viewGoodsContainers[i].setVisibility(View.VISIBLE);
            mImageLoader.displayImage(detailGoods.getCover(), workViewHolder.goodsCovers[i], ImageOptions.options);
            workViewHolder.goodsNames[i].setText(detailGoods.getTitle());
            String price = CurrencyUtil.getMoney(detailGoods.getBasic_price());
            workViewHolder.goodsPrices[i].setText(TextUtils.isEmpty(price) ? "" : String.format(mContext.getString(R.string.price_format), price));
            workViewHolder.viewGoodsContainers[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    gotoUtil.gotoAct(mContext, GoodsDetailActivity.class, detailGoods.getZg_id());
                }
            });
        }
        for (int i = 2; i - size >= 0; i--) {
            workViewHolder.viewGoodsContainers[i].setVisibility(View.INVISIBLE);
        }
        workViewHolder.workTopView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, CircleSmoothActivity.class);
                intent.putExtra(gotoUtil.INTENT_VALUE_ONE, goodsChoiceData.getReal_name());
                intent.putExtra(gotoUtil.INTENT_VALUE_TWO, goodsChoiceData.getId());
                intent.putExtra("from", 3);
                mContext.startActivity(intent);
            }
        });
    }

    private void dealGoodsData(final GoodsChoiceItem goodsChoiceItem, ViewHolder viewHolder, int pos) {
        mImageLoader.displayImage(goodsChoiceItem.getCover(), viewHolder.pics[pos], ImageOptions.options);
        viewHolder.titles[pos].setText(goodsChoiceItem.getTitle());
        String uname = goodsChoiceItem.getUname();
        viewHolder.names[pos].setText(TextUtils.isEmpty(uname) ? "" : String.format(mContext.getString(R.string.power_by), uname));
        String price = CurrencyUtil.getMoney(goodsChoiceItem.getBasic_price());
        viewHolder.prices[pos].setText(TextUtils.isEmpty(price) ? "" : String.format(mContext.getString(R.string.price_format), price));
        ((View) viewHolder.pics[pos].getParent()).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoUtil.gotoAct(mContext, GoodsDetailActivity.class, goodsChoiceItem.getZg_id());
            }
        });
    }

    private class ViewHolder {

        public ImageView[] pics;
        public TextView[] titles;
        public TextView[] names;
        public TextView[] prices;

        public ViewHolder(View convertView) {
            pics = new ImageView[]{(ImageView) convertView.findViewById(R.id.goods_pic_one), (ImageView) convertView.findViewById(R.id.goods_pic_two)};
            titles = new TextView[]{(TextView) convertView.findViewById(R.id.goods_title_one), (TextView) convertView.findViewById(R.id.goods_title_two)};
            names = new TextView[]{(TextView) convertView.findViewById(R.id.goods_user_one), (TextView) convertView.findViewById(R.id.goods_user_two)};
            prices = new TextView[]{(TextView) convertView.findViewById(R.id.goods_price_one), (TextView) convertView.findViewById(R.id.goods_price_two)};
        }
    }

    private class WorkViewHolder {
        View workTopView;
        ImageView ivWorkCover;
        TextView tvWorkName;
        TextView tvGoodsCount;
        View viewGoodsOne;
        View viewGoodsTwo;
        View viewGoodsThree;
        ImageView[] goodsCovers;
        TextView[] goodsNames;
        TextView[] goodsPrices;
        View[] viewGoodsContainers;

        public WorkViewHolder(View convertView) {
            workTopView = convertView.findViewById(R.id.work_topview);
            ivWorkCover = (ImageView) convertView.findViewById(R.id.iv_work_cover);
            tvWorkName = (TextView) convertView.findViewById(R.id.tv_work_name);
            tvGoodsCount = (TextView) convertView.findViewById(R.id.tv_goods_count);
            viewGoodsOne = convertView.findViewById(R.id.view_goods_one);
            viewGoodsTwo = convertView.findViewById(R.id.view_goods_two);
            viewGoodsThree = convertView.findViewById(R.id.view_goods_three);
            viewGoodsContainers = new View[]{viewGoodsOne, viewGoodsTwo, viewGoodsThree};
            goodsCovers = new ImageView[]{(ImageView) viewGoodsOne.findViewById(R.id.iv_goods_cover),
                    (ImageView) viewGoodsTwo.findViewById(R.id.iv_goods_cover), (ImageView) viewGoodsThree.findViewById(R.id.iv_goods_cover)};
            goodsNames = new TextView[]{(TextView) viewGoodsOne.findViewById(R.id.tv_work_goods_name),
                    (TextView) viewGoodsTwo.findViewById(R.id.tv_work_goods_name), (TextView) viewGoodsThree.findViewById(R.id.tv_work_goods_name)};
            goodsPrices = new TextView[]{(TextView) viewGoodsOne.findViewById(R.id.tv_work_goods_price),
                    (TextView) viewGoodsTwo.findViewById(R.id.tv_work_goods_price), (TextView) viewGoodsThree.findViewById(R.id.tv_work_goods_price)};
        }
    }

    private class SpecialViewHolder {
        ImageView ivSpecialCover;
        TextView tvSpecialName;

        public SpecialViewHolder(View convertView) {
            ivSpecialCover = (ImageView) convertView.findViewById(R.id.iv_special_cover);
            tvSpecialName = (TextView) convertView.findViewById(R.id.tv_special_name);
        }
    }
}
